package com.xiaomaoguai;

import com.google.common.base.Preconditions;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;

/**
 * @Description :
 * @FileName: Test.java
 * @Author :WeiHui.Zhang
 * @Data : 2015年11月4日 下午5:21:50
 * @Version:V1.00
 */
public class Test {

	public static void main(String[] args) throws IOException {

		String configFile = "classpath:log4j.properties";

		InputStream openStream = getConfigFilePath(configFile).openStream();

		Properties properties = new Properties();

		properties.load(openStream);

		System.out.println(properties.get("log4j.appender.stdout"));
	}

	public static URL getConfigFilePath(String configFile) throws MalformedURLException {
		Preconditions.checkArgument(StringUtils.isNotBlank(configFile), "配置文件路径不能为空");
		String path = null;
		int indexOf = configFile.indexOf(RESOURCE_CLASSPATH_PREFIEX);

		if (indexOf > -1) {
			String substring = configFile.substring(RESOURCE_CLASSPATH_PREFIEX.length());

			path = Test.class.getResource("/") + substring;
		} else {
			path = Test.class.getResource("") + configFile;
		}
		return new URL(path);
	}

	private static final String RESOURCE_CLASSPATH_PREFIEX = "classpath:";
}
